﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TicketingCompany
{
    public partial class PurchaseTicket : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["id"]==null)
            {
                Response.Redirect("~/Purchases.aspx");
            }
            if (!Page.IsPostBack)
            {
                Event ev = GetOperations.GetEventByID(int.Parse(Request["id"]));
                LiteralEventInfo.Text += ev.Title + "<br/>";
                LiteralEventInfo.Text += ev.Price + "<br/>";
                LiteralEventInfo.Text += ev.DateTime + "<br/>";
                TextBoxTickets.Text = "1";                
            }
        }

        protected void ButtonPurchase_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Purchase pur = new Purchase
                {
                    EventID = Convert.ToInt32(Request["id"]),
                    CustomerName = TextBoxUserName.Text,
                    ShippingAddress = TextBoxShippingAdress.Text,
                    NumberOfTickets = int.Parse(TextBoxTickets.Text),
                    Delivered = false
                };
                GetOperations.CreatePurchase(pur);
                LabelIsAdded.Visible = true;
                LabelIsAdded.Text += " Your Purchase Number is: #" + pur.PurchaseID;
            }            
        }

        protected void CustomValidatorShippingAddress_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = TextBoxShippingAdress.Text.Length < 150 && TextBoxShippingAdress.Text.Length > 10;
        }
    }
}